package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class SearchController {
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private ISearchService searchService;


    @RequestMapping("/q")
    public String search(@ModelAttribute("qo") SearchQueryObject qo, Model model) {
        String viewName = null;
        if (StringUtils.isEmpty(qo.getKeyword())) {
            return "redirect:/";
        }
        switch (qo.getType()) {
            case SearchQueryObject.SEARCH_DEST:
                viewName = "searchDest";
                searchDest(qo, model);
                break;
            case SearchQueryObject.SEARCH_STRATEGY:
                viewName = "searchStrategy";
                searchStrategy(qo, model);
                break;
            case SearchQueryObject.SEARCH_TRAVEL:
                viewName = "searchTravel";
                searchTravel(qo, model);
                break;
            case SearchQueryObject.SEARCH_USER:
                viewName = "searchUser";
                searchUser(qo, model);
                break;
            default:
                viewName = "searchAll";
                searchAll(qo, model);
        }
        return "index/" + viewName;
    }

    private void searchAll(SearchQueryObject qo, Model model) {

        int total = 0;
        //
        Page<StrategyTemplate> strategys = searchService.hightLightSearch(StrategyTemplate.INDEX_TYPE_NAME, StrategyTemplate.class, qo,
                "title", "subTitle", "summary");
        total += strategys.getTotalElements();
        //
        Page<TravelTemplate> travels = searchService.hightLightSearch(TravelTemplate.INDEX_TYPE_NAME, TravelTemplate.class, qo,
                "title", "summary");
        total += travels.getTotalElements();
        //
        Page<UserInfoTemplate> userInfos = searchService.hightLightSearch(UserInfoTemplate.INDEX_TYPE_NAME, UserInfoTemplate.class, qo,
                "nickname", "city");
        total += userInfos.getTotalElements();
        //
        Page<DestinationTemplate> destinations = searchService.hightLightSearch(DestinationTemplate.INDEX_TYPE_NAME, DestinationTemplate.class, qo,
                "name", "info");
        total += destinations.getTotalElements();

        //封装到查询结果集中
        SearchResult result = new SearchResult();
        result.setStrategys(strategys.getContent());
        result.setTravels(travels.getContent());
        result.setUsers(userInfos.getContent());
        result.setDests(destinations.getContent());
        result.setTotal(total);
        model.addAttribute("data", result);

    }


    private void searchDest(SearchQueryObject qo, Model model) {
        //dest 当前目的地
        model.addAttribute("dest", destinationSearchService.getByName(qo.getKeyword()));

        //data :当前的搜索结果
        int num = 1;
        List<StrategyTemplate> sts = strategySearchService.listByDestName(qo.getKeyword());
        num += sts.size();
        List<TravelTemplate> tts = travelSearchService.listByDestName(qo.getKeyword());
        num += tts.size();
        List<UserInfoTemplate> uts = userInfoSearchService.listByCity(qo.getKeyword());
        num += uts.size();

        //封装到查询结果集中
        SearchResult result = new SearchResult();
        result.setStrategys(sts);
        result.setTravels(tts);
        result.setUsers(uts);
        result.setTotal(num);
        model.addAttribute("data", result);
    }

    private void searchStrategy(SearchQueryObject qo, Model model) {
        Page<StrategyTemplate> page = searchService.hightLightSearch(StrategyTemplate.INDEX_TYPE_NAME, StrategyTemplate.class, qo,
                "title", "subTitle", "summary");
        model.addAttribute("page", page);
    }

    private void searchTravel(SearchQueryObject qo, Model model) {
        Page<TravelTemplate> page = searchService.hightLightSearch(TravelTemplate.INDEX_TYPE_NAME, TravelTemplate.class, qo,
                "title", "summary");
        model.addAttribute("page", page);
    }

    private void searchUser(SearchQueryObject qo, Model model) {
        Page<UserInfoTemplate> page = searchService.hightLightSearch(UserInfoTemplate.INDEX_TYPE_NAME, UserInfoTemplate.class, qo,
                "nickname", "city");
        model.addAttribute("page", page);
    }
}
